﻿//----------------------------------------------------------------------------------------------------
// The Binding manager allows the rmeote execution of functions (formteh console, usually)
//----------------------------------------------------------------------------------------------------

package HG.Managers
{
	import HG.Debug;
	
	public class BindingManager extends IGameManager
	{
		var m_bindings:Array
		
		//----------------------------------------------------------------------------------------------------
		public function BindingManager()
		{
			m_bindings = new Array();
		}
			
		//----------------------------------------------------------------------------------------------------
		override public function Initialize()
		{
		}
		
		//----------------------------------------------------------------------------------------------------
		override public function Shutdown()
		{
			
		}
		
		//----------------------------------------------------------------------------------------------------
		override public function OnTick(DeltaTime:Number)
		{
			
		}
		
		//----------------------------------------------------------------------------------------------------
		public function RegisterBinding(binding:String, func:Function)
		{
			m_bindings[binding] = func;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function DoBinding(binding:String, params:Array)
		{
			if (!m_bindings[binding])
			{
				Debug.Trace("Error: Binding not found: \"" + binding + "\"");
				return;
			}
			
			m_bindings[binding].apply(this, params);
		}
		
		//----------------------------------------------------------------------------------------------------
		public function HandleInput(input:String)
		{
			var split:Array = input.split('(');
			var binding:String = split[0];
			
			var par:String = "";
			if (split.length > 1)
			{
				par = split[1];
				split = par.split(')');
				par = split[0];
			}
			
			var paramaters:Array = par.split(',');
				
			DoBinding(binding, paramaters);
		}
		
		//----------------------------------------------------------------------------------------------------
		
	}
}